package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.risk;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.constant.accept.*;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.InsurantModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.RequestModel;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.InsuredModel;
import com.bmps.springcloud.insuredmicroservice.repository.underwrite.coresystem.InsuredCoreRepository;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author Administrator
 * @date 2020/12/22 17:35
 */
@Component
public class Llt030InsuranceRiskStrategy implements InsuranceRiskStrategy{
    private final InsuredCoreRepository insuredCoreRepository;

    private final RedisService redisService;

    public Llt030InsuranceRiskStrategy(InsuredCoreRepository insuredCoreRepository, RedisService redisService) {
        this.insuredCoreRepository = insuredCoreRepository;
        this.redisService = redisService;
    }
    @Override
    public InsuredModel checkProductRiskQuota(ProductModel product, RequestModel requestModel) {
        InsurantModel insurantModel = AcceptUtil.getInsurantModelBySeq(product.getProdInsSeq(), requestModel.getCustInfo().getInsurants().getInsurant());
        Date valiDate = requestModel.getPolicyInfo().getCvalidate();
        int years = DateUtil.getYears(insurantModel.getBirthday(), valiDate);

        BigDecimal currentAmnt = product.getAmnt();

        //缓存中可能存在的保额
        BigDecimal cacheQuota = redisService.getCacheQuota(QuotaConstant.SORT_LLT030, insurantModel.getIdNo(), currentAmnt, requestModel.getPolicyInfo().getContNo());

        BigDecimal historicalSumPrem = StringUtils.isNotBlank(insurantModel.getInsuredNo()) &&
                !StringUtils.equals(AcceptConstant.MINUS_ONE, insurantModel.getInsuredNo()) ?
                insuredCoreRepository.getHistoricalSumAmnt(insurantModel.getInsuredNo(), product.getProdCode()) : BigDecimal.valueOf(0);
        //累计险种保额
        BigDecimal total = currentAmnt.add(cacheQuota).add(historicalSumPrem);
        Integer code ;
        if (StringUtils.isBlank(insurantModel.getCity()) || AgencyConstant.MY0201.equals(requestModel.getPolicyInfo().getAgencyCode())) {
            code = 3;
        }else {
            code = redisService.getCity(insurantModel.getCity());
        }
        boolean EIGHTEEN_AND_FORTY = (years >= 18 && years <= 40);
        boolean FORTY_AND_FIFTY = (years >= 41 && years <= 50);
        boolean EIGHTEEN_AND_SIXTY = (years >= 18 && years <= 60);

        if (EIGHTEEN_AND_SIXTY ) {
            if (EIGHTEEN_AND_FORTY && code==DigitalConstant.CITY_ONE) {
                if (total.compareTo(BigDecimal.valueOf(UnderWriteConstant.TWO_HUNDRED_THOUSAND))>0) {
                    return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_FAIL_CODE,
                            String.format(UnderWriteConstant.UNDER_WRITE_NYGG0058_MESSAGE, insurantModel.getName(), years,"20"));
                }
            } else if ((FORTY_AND_FIFTY && code == DigitalConstant.CITY_ONE) || (EIGHTEEN_AND_FORTY && code == DigitalConstant.CITY_TWO)) {
                if (total.compareTo(BigDecimal.valueOf(UnderWriteConstant.ONE_HUNDRED_AND_FIFTY_THOUSAND)) > 0) {
                    return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_FAIL_CODE,
                            String.format(UnderWriteConstant.UNDER_WRITE_NYGG0058_MESSAGE, insurantModel.getName(), years,"15"));
                }
            } else {
                if (total.compareTo(BigDecimal.valueOf(UnderWriteConstant.ONE_HUNDRED_THOUSAND)) > 0) {
                    return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_FAIL_CODE,
                            String.format(UnderWriteConstant.UNDER_WRITE_NYGG0058_MESSAGE, insurantModel.getName(), years,"10"));
                }
            }
        }

        if (total.compareTo(BigDecimal.valueOf(UnderWriteConstant.ONE_MILLION)) > 0) {
            return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_FAIL_CODE,
                    String.format(UnderWriteConstant.UNDER_WRITE_NYGG0055_MESSAGE, insurantModel.getName(), years));
        }
        return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_SUCCESS_CODE, AcceptConstant.RESULT_SUCCESS_MESSAGE);
    }
}
